# read in data
library(stringr)
library(data.table)
library(ggplot2)
library(dplyr)
library(lubridate)
library(lfe)
library(sjPlot)
library(stargazer)
library(standardize)
library(broom)
library(fixest)

############## set your wd
setwd()
###### approval

stateinfo <- read.csv("~Data/ManyStateCodes.csv")
ICPSRSen <- read.csv("~Data/SenatorICPSR.csv")

ICPSRHouse <- read.csv("~Data/HouseICPSR.csv")


cceslong <- fread("~Data/cces-joined-long.csv")
cceslong <- filter(cceslong, CurrentSenName != "")
cces <- fread("~Data/cces-joined.csv")
cces <- filter(cces, CurrentHouseName != "")

cceslong <- merge(cceslong, ICPSRSen, by.x = "CurrentSenName", by.y = "Senator")
cces <- merge(cces, ICPSRHouse, by.x = "CurrentHouseName", by.y = "Member")

cceslong$S_Approve <- ifelse(cceslong$Sen_Approve < 3 , 1,0)
cceslong$S_Approve <- ifelse(cceslong$Sen_Approve > 4 , NA, cceslong$S_Approve)
cceslong$Sen_known <- ifelse(cceslong$Sen_Approve == 5 , 0, 1)
cceslong$Resp_Party <- ifelse(cceslong$pid7 > 4 , "R","D")
cceslong$Resp_Party <- ifelse(cceslong$pid7 == 4 , "I",cceslong$Resp_Party)
cceslong$Resp_Party <- ifelse(cceslong$pid7 == 8 , "I",cceslong$Resp_Party)
cceslong$Resp_Party <- ifelse(cceslong$pid7 > 9 , NA,cceslong$Resp_Party)
cceslong$Resp_Party3 <- ifelse(cceslong$pid3 == 1 , "D", "I")
cceslong$Resp_Party3 <- ifelse(cceslong$pid3 == 2 , "R", cceslong$Resp_Party3)                            
cceslong$news <- ifelse(cceslong$newsint < 3,1,0)

cces$Mem_Approve <- ifelse(cces$House_Approve < 3 , 1,0)
cces$Mem_Approve <- ifelse(cces$House_Approve > 4 , NA, cces$Mem_Approve)
cces$House_known <- ifelse(cces$House_Approve == 5 , 0, 1)
cces$Resp_Party <- ifelse(cces$pid7 > 4 , "R","D")
cces$Resp_Party <- ifelse(cces$pid7 == 4 , "I",cces$Resp_Party)
cces$Resp_Party <- ifelse(cces$pid7 == 8 , "I",cces$Resp_Party)
cces$Resp_Party <- ifelse(cces$pid7 > 9 , NA,cces$Resp_Party)
cces$Resp_Party3 <- ifelse(cces$pid3 == 1 , "D", "I")
cces$Resp_Party3 <- ifelse(cces$pid3 == 2 , "R", cces$Resp_Party3)    
cces$news <- ifelse(cces$newsint < 3,1,0)

cceslong$icpsrcongress <- paste0(as.character(cceslong$icpsr),"-", as.character(cceslong$congress))
cces$icpsrcongress <- paste0(as.character(cces$icpsr),"-", as.character(cces$congress))

################# Load Tweets and FB

congressFBlocal <- readRDS("~Data/congressfb_aggregated_RR.rds")
congresstweetslocal <- readRDS("~Data/congresstweets_aggregated_RR.rds")

####### process and merge

senatetweets <- filter(congresstweetslocal, chamber.x == "Senate")
housetweets <- filter(congresstweetslocal, chamber.x == "House")

senateFB <- filter(congressFBlocal, chamber.x == "Senate")
houseFB <- filter(congressFBlocal, chamber.x == "House")

senlocal <- select(senatetweets, tweets, local, party, PVIABS, PVIParty, Freshman, Seniority, republican, NPdist, Vote_Pct_2PT, icpsrcongress, Cmte_Chair, elect_year, PVIAbsscale)
houselocal <- select(housetweets, tweets, local, party, PVIABS, PVIParty, Freshman, Seniority, republican, NPdist, Vote_Pct_2PT, icpsrcongress, Cmte_Chair , elect_year, PVIAbsscale)

senlocalFB <- select(senateFB, tweets, local, party, PVIABS, PVIParty, Freshman, Seniority, republican, NPdist, Vote_Pct_2PT, icpsrcongress, Cmte_Chair , elect_year, PVIAbsscale)
houselocalFB <- select(houseFB, tweets, local, party, PVIABS, PVIParty, Freshman, Seniority, republican, NPdist, Vote_Pct_2PT, icpsrcongress, Cmte_Chair , elect_year, PVIAbsscale)

senlocal <- merge(cceslong, senlocal, by = "icpsrcongress")
houselocal <- merge(cces, houselocal, by = "icpsrcongress")
senlocalFB <- merge(cceslong, senlocalFB, by = "icpsrcongress")
houselocalFB <- merge(cces, houselocalFB, by = "icpsrcongress")

senlocal$partymatch <- ifelse(senlocal$Resp_Party3 == senlocal$party, 1, 0)
senlocal$partyopposite <- ifelse(senlocal$partymatch == 0 & senlocal$Resp_Party3 != "I", 1, 0)
senlocal$election <- ifelse(senlocal$Vote_Pct_2PT > 0,1,0)

houselocal$partymatch <- ifelse(houselocal$Resp_Party == houselocal$party, 1, 0)
houselocal$partyopposite <- ifelse(houselocal$partymatch == 0 & houselocal$Resp_Party3 != "I", 1, 0)
houselocal$election <- ifelse(houselocal$Vote_Pct_2PT > 0,1,0)

senlocalFB$partymatch <- ifelse(senlocalFB$Resp_Party3 == senlocalFB$party, 1, 0)
senlocalFB$partyopposite <- ifelse(senlocalFB$partymatch == 0 & senlocalFB$Resp_Party3 != "I", 1, 0)
senlocalFB$election <- ifelse(senlocalFB$Vote_Pct_2PT > 0,1,0)

houselocalFB$partymatch <- ifelse(houselocalFB$Resp_Party == houselocalFB$party, 1, 0)
houselocalFB$partyopposite <- ifelse(houselocalFB$partymatch == 0 & houselocalFB$Resp_Party3 != "I", 1, 0)
houselocalFB$election <- ifelse(houselocalFB$Vote_Pct_2PT > 0,1,0)

# model setup

senlocal$congressparty <- paste0(as.character(senlocal$congress),"-", as.character(senlocal$party))
houselocal$congressparty <- paste0(as.character(houselocal$congress),"-", as.character(houselocal$party))
senlocalFB$congressparty <- paste0(as.character(senlocalFB$congress),"-", as.character(senlocalFB$party))
houselocalFB$congressparty <- paste0(as.character(houselocalFB$congress),"-", as.character(houselocalFB$party))

senlocal$approve <- senlocal$S_Approve
senlocalFB$approve <- senlocalFB$S_Approve
houselocal$approve <- houselocal$Mem_Approve
houselocalFB$approve <- houselocalFB$Mem_Approve
senlocal$known <- senlocal$Sen_known
senlocalFB$known <- senlocalFB$Sen_known
houselocal$known <- houselocal$House_known
houselocalFB$known <- houselocalFB$House_known

senlocal$chamber <- "Senate"
senlocalFB$chamber <- "Senate"
houselocal$chamber <- "House"
houselocalFB$chamber <- "House"


senlocal <- select(senlocal, approve, known, local, PVIABS, news, Freshman, Seniority, partymatch, partyopposite, educ ,NPdist, congressparty, commonweight, icpsr, Vote_Pct_2PT, chamber, Cmte_Chair, elect_year, PVIAbsscale)
senlocalFB <- select(senlocalFB, approve, known, local, PVIABS, news, Freshman, Seniority, partymatch, partyopposite, educ ,NPdist, congressparty, commonweight, icpsr, Vote_Pct_2PT, chamber, Cmte_Chair, elect_year, PVIAbsscale)
houselocal <- select(houselocal, approve, known, local, PVIABS, news, Freshman, Seniority, partymatch, partyopposite, educ ,NPdist, congressparty, commonweight, icpsr, Vote_Pct_2PT, chamber, Cmte_Chair, elect_year, PVIAbsscale)
houselocalFB <- select(houselocalFB, approve, known, local, PVIABS, news, Freshman, Seniority, partymatch, partyopposite, educ ,NPdist, congressparty, commonweight, icpsr, Vote_Pct_2PT,chamber, Cmte_Chair, elect_year, PVIAbsscale)

local <- rbind(senlocal, houselocal)
localFB <- rbind(senlocalFB, houselocalFB)

local$election <- ifelse(is.na(local$Vote_Pct_2PT), 0, 1)
localFB$election <- ifelse(is.na(localFB$Vote_Pct_2PT), 0, 1)


############ models

mod1 <- felm(approve ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year + chamber| congressparty, data = local, weights=local$commonweight)
mod2 <- felm(known ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year + chamber| congressparty, data = local, weights=local$commonweight)
mod3 <- felm(approve ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year + chamber| congressparty, data = localFB, weights=localFB$commonweight)
mod4 <- felm(known ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year + chamber| congressparty, data = localFB, weights=localFB$commonweight)

tab_model(mod1, mod2, mod3, mod4)
stargazer(mod1, mod2, mod3, mod4, type = "html", out = "LocalApprovalAll.htm")

mod1 <- felm(approve ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year + chamber + scale(local)*PVIAbsscale| congressparty, data = local, weights=local$commonweight)
mod2 <- felm(known ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year + chamber + scale(local)*PVIAbsscale| congressparty, data = local, weights=local$commonweight)
mod3 <- felm(approve ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year + chamber + scale(local)*PVIAbsscale| congressparty, data = localFB, weights=localFB$commonweight)
mod4 <- felm(known ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year + chamber + scale(local)*PVIAbsscale| congressparty, data = localFB, weights=localFB$commonweight)

tab_model(mod1, mod2, mod3, mod4)
stargazer(mod1, mod2, mod3, mod4, type = "html", out = "LocalApprovalAll_Interaction.htm")


############## within-member

mod1 <- felm(approve ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year| congressparty + icpsr, data = local, weights=local$commonweight)
mod2 <- felm(known ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year| congressparty + icpsr, data = local, weights=local$commonweight)
mod3 <- felm(approve ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year| congressparty + icpsr, data = localFB, weights=localFB$commonweight)
mod4 <- felm(known ~ scale(local) + PVIAbsscale + news + Freshman + Seniority + Cmte_Chair + partymatch + partyopposite + educ + NPdist + elect_year| congressparty + icpsr, data = localFB, weights=localFB$commonweight)

tab_model(mod1, mod2, mod3, mod4)
stargazer(mod1, mod2, mod3, mod4, type = "html", out = "LocalApprovalAllWithin.htm")

################# plot

coefs <- read.csv("~Data/LocalElectionCoefs.csv")

coefs <- filter(coefs, Question == "Approval" | Question == "Known")

ggplot(coefs, aes(x = Question, y = coef, color = Measure)) +
  geom_point(position = position_dodge(.5), size = 2.5) +
  geom_errorbar(aes(ymin = lower, ymax = upper),
                position = position_dodge(.5),
                width = 0.2) +
  scale_color_manual(values = c("grey60", "grey20"))+
  geom_hline(yintercept=0, linetype="dashed", color = "grey50")+
  ylab("Difference in Probability Based on Localized Messaged Pct.")+
  theme_bw() +
  ylim(-0.01,0.01)+
  theme(
    legend.position = "bottom",
    axis.title.x = element_blank())+
  facet_wrap(~Model)
ggsave(filename = "Local Approval Effects.png")


